#
#   author: shenyc (shenyczz@163.com)
# 
# #########################################################
# 
from datetime import date, datetime, timedelta, timezone
from .micaps_Enums import FormatCode


class Station(object):
    """
    站点类
    """

    def __init__(self):
        self.id: str = ""
        self.name: str = ""
        self.lon: float = 0
        self.lat: float = 0
        self.alt: float = 0
        self.value: float = 0
    # end__init__

    # @property
    # def id(self) -> str:
    #     return self.id

    # @id.setter
    # def id(self, value):
    #     if not isinstance(value, str):
    #         raise ValueError('score must be an integer!')
    #     # if value < 0 or value > 100:
    #     #     raise ValueError('score must between 0 ~ 100!')
    #     self.id = value


class DataInfo(object):
    """
    数据信息
    """

    def __init__(self):
        self.comment: str = ""  # 注释
        self.formatCode: FormatCode = FormatCode.NONE
        self.dateTime: datetime = datetime.now
        self.timePeriod: int = 0
        self.stationCount: int = 0
    # end__init__
    pass


class MicapsFile(object):
    """
    micaps file class
    """

    def __init__(self, fname: str = None):
        self._fname = fname
        self._datainfo: DataInfo = DataInfo()    # 数据信息
        self._stations: list[Station] = []       # 站点信息

        if(fname != None):
            self._datainfo, self._stations = self.load_micaps_file_format_03(
                fname)
        # endif

    # end__init__

    @property
    def FileName(self) -> str: return self._fname

    @property
    def Datainfo(self) -> DataInfo: return self._datainfo

    @property
    def Stations(self) -> list[Station]: return self._stations

    def load_micaps_file_format_03(self, fname: str) -> tuple[DataInfo, list[Station]]:
        """
        装载 MICAPS 第3类文件

        内容:
            文件头: 
                diamond 3 2018年6月1日08时00分00秒黄河流域站点24小时降水 \n
                2018 6 1 0 0 \n
                0 0 0 0 1 \n
                6734 \n
            数据: 
                861002  108.299   36.780   1245.0      0.0
        """
        # fname = rf"G:\R24\201806\2018060108.000"

        alllines = ""
        with open(fname, 'r') as infile:
            alllines = infile.readlines()
        # end with
        
        temp: str = "\n".join(alllines)
        temp = temp.replace("\r", " ")
        temp = temp.replace("\n", " ")
        content = temp.split()

        # DataInfo
        #
        datainfo = DataInfo()
        #
        # diamond 3 2018年6月1日08时00分00秒黄河流域站点24小时降水
        index: int = 0
        x = content[index]  # diamond
        index += 1
        datainfo.formatCode = FormatCode(int(content[index]))  # 3
        index += 1
        datainfo.comment = content[index]  # comment
        # 2018 6 1 0 0 \n
        index += 1
        year = int(content[index])  # year
        index += 1
        month = int(content[index])  # month
        index += 1
        day = int(content[index])  # day
        index += 1
        hour = int(content[index])  # hour
        datainfo.dateTime = datetime(year, month, day, hour)
        index += 1
        datainfo.timePeriod = int(content[index])  # 时次
        # 0 0 0 0 1
        index += 1
        x1 = int(content[index])    #
        index += 1
        x2 = int(content[index])    #
        index += 1
        x3 = int(content[index])    #
        index += 1
        x4 = int(content[index])    #
        index += 1
        x5 = int(content[index])    #
        # 6734
        index += 1
        datainfo.stationCount = int(content[index])  # 站点数量

        # list[Station]
        #
        stations: list[Station] = []
        for n in range(datainfo.stationCount):
            # 861002  108.299   36.780   1245.0      0.0
            # 每行站点有5个数据
            i = (index+1) + n*5
            station = Station()
            station.id = str(content[i+0])
            station.lon = float(content[i+1])
            station.lat = float(content[i+2])
            station.alt = float(content[i+3])
            station.value = float(content[i+4])
            stations.append(station)
        # endfor

        self._datainfo = datainfo
        self._stations = stations
        return datainfo, stations


# class MicapsFile(object)
